mui.init();
var classification; //分类
var name;
var id_num, cid, cid_two;
$(function() {
	$("img.lazy").lazyload({
		effect: "fadeIn",
		placeholder: "../../images/as.jpg",
		container: $("#content1List"),
		failurelimit: 10
	});
});
//左边商品分类
$(function() {
	mui.ajax(goods_url + '/goods/itemCatList', {
		data: {},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			if(data.code == 10000) {
				classification = data.resp[0].id;
				name = data.resp[0].name;
				$(".more").attr("name", name);
				var html = '';
				for(var i = 0; i < data.resp.length; i++) {
					html += '<a class="mui-control-item" id_num = ' + data.resp[i].id + ' name=' + data.resp[i].name + '>' + data.resp[i].name + '</a>';
				}
				$("#segmentedControls").append(html);
				//第一个子元素默认选中
				$("#segmentedControls").find("a").first().addClass("mui-active");

				itemList(classification)
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	})
})

//第一个选中
function itemList(classification) {
	mui.ajax(goods_url + '/goods/itemTwoCats', {
		data: {
			"id": classification, //分类 
			"sign": "string" //密钥
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.resp == "") {
				var html = "";
				$("#content1List").html('<div style=line-height:400px;text-align:center;color:#C0C0C0;font-size:16px;>暂无商品</div>');
			} else {
				if(data.resp.length != 0) {
					id = data.resp[0].parent_id;

					var html = "";
					for(var i = 0; i < data.resp.length; i++) {
						html += '<li class="mui-table-view-cell">';
						html += '<a class="mui-navigate-right more" mores="' + data.resp[i].id + '" name="' + data.resp[i].name + '">' + data.resp[i].name + '</a>';
						html += '<div class="mui-row row_div">';
						if(data.resp[i].zitemCatThreeVOList.length == 0) {
							html += '<div style=line-height:400px;text-align:center;color:#C0C0C0;font-size:16px;>暂无商品信息</div>';
						} else {
							for(var j = 0; j < data.resp[i].zitemCatThreeVOList.length; j++) {
								html += '<div class="mui-col-xs-4 row_divs" moress="' + data.resp[i].zitemCatThreeVOList[j].id + '" name="' + data.resp[i].zitemCatThreeVOList[j].name + '">';;
								html += '<img  class="lazy" width="100%" data-original="' + data.resp[i].zitemCatThreeVOList[j].url + data.resp[i].zitemCatThreeVOList[j].icon + '"/>';
								html += '<p class="mui-ellipsis">' + data.resp[i].zitemCatThreeVOList[j].name + '</p>';
								html += '</div>';
							}
						}

						html += '</div>';
					}
					$("#content1List").html(html);

					$("img.lazy").lazyload({
						effect: "fadeIn",
						placeholder: "../../images/as.jpg",
						container: $("#content1List"),
						failurelimit: 10
					});

				}
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	})
}
//点击执行
$(document).on("tap", "#segmentedControls a", function() {
	id = $(this).attr("id_num");
	name = $(this).attr("name");
	$(".more").attr("name", name)
	$(".more").attr("more", id)
	mui.ajax(goods_url + '/goods/itemTwoCats', {
		data: {
			"id": id, //分类 
			"sign": "string" //密钥
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			if(data.resp.length != 0) {
				var html = "";
				for(var i = 0; i < data.resp.length; i++) {
					html += '<li class="mui-table-view-cell">';
					html += '<a class="mui-navigate-right more" mores="' + data.resp[i].id + '" name="' + data.resp[i].name + '">' + data.resp[i].name + '</a>';
					html += '<div class="mui-row row_div">';
					if(data.resp[i].zitemCatThreeVOList.length == 0) {
						html += '<div style=line-height:400px;text-align:center;color:#C0C0C0;font-size:16px;>暂无商品信息</div>';
					} else {
						for(var j = 0; j < data.resp[i].zitemCatThreeVOList.length; j++) {
							html += '<div class="mui-col-xs-4 row_divs" moress="' + data.resp[i].zitemCatThreeVOList[j].id + '" name="' + data.resp[i].zitemCatThreeVOList[j].name + '">';
							html += '<img class="lazy" width="100%" data-original="' + data.resp[i].zitemCatThreeVOList[j].url + data.resp[i].zitemCatThreeVOList[j].icon + '" />';
							html += '<p class="mui-ellipsis">' + data.resp[i].zitemCatThreeVOList[j].name + '</p>';
							html += '</div>';
						}
					}

					html += '</div>';
				}
				$("#content1List").html(html);

				$("img.lazy").lazyload({
					effect: "fadeIn",
					placeholder: "../../images/as.jpg",
					container: $("#content1List"),
					failurelimit: 10
				});

			} else if(data.resp.length == 0) {
				var html = "";
				$("#content1List").html('<div style=line-height:400px;text-align:center;color:#C0C0C0;font-size:16px;>暂无商品</div>');

			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	})
});

//点击二级
$(document).on("tap", ".more", function() {
	name = $(this).attr("name");
	cid_two = $(this).attr("mores");
	mui.openWindow({
		url: "classification_more.html",
		extras: {
			name: name,
			cid_two: cid_two
		}
	});
});
//点击三级
$(document).on("tap", ".row_divs", function() {
	name = $(this).attr("name");
	cid = $(this).attr("moress");
	mui.openWindow({
		url: "classification_more.html",
		extras: {
			name: name,
			cid: cid
		}
	});
});
//点击搜索
document.getElementById("search_btn").addEventListener("tap", function() {
	mui.openWindow({
		url: "search.html",
		id: "search"
	});
});

//去购买
//$(document).on("tap", ".to_buy", function() {
//	var commodity_id = $(this).attr("leaber_id");
//	mui.openWindow({
//		url: "commodity_details.html",
//		extras: {
//			commodity_id: commodity_id
//		}
//	})
//});